#include "Client.h"

#include "Logging.h"

#include <vector>
#include <thread>

// std::string server_addr = "192.168.11.56";
std::string server_addr = "127.0.0.1";
int default_port = 8888;
int main(int argc, char *argv[])
{
    muduo::Logger::setLogLevel(Logger::LogLevel::INFO);

    if (argc == 2)
    {
        default_port = atoi(argv[1]);
    }

    Client *myClient = nullptr;
    EventLoop *myLoop = nullptr;

    std::thread runthread = std::thread(
        [&myClient, &myLoop]()
        {
            InetAddress addr(server_addr, default_port);
            myLoop = new EventLoop;
            myClient = new Client(myLoop, &addr);
            myClient->Start();
            myLoop->loop();

            delete myLoop;
        });

    ::sleep(1);

    myClient->SendMsg("123456");
    myClient->SendMsg("abcdefg");

    ::sleep(1);

    myClient->Close();

    delete myClient;

    ::sleep(1);

    myLoop->quit();
    if (runthread.joinable())
    {
        runthread.join();
    }

    printf("thread exit succcess..\n");
    getchar();

    return 0;
}

/*
int main(int argc, char *argv[])
{
    std::vector<int> ports;
    if (argc < 2)
    {
        ports.push_back(default_port);
    }
    else
    {

        for (int i = 0; i < argc; ++i)
        {
            ports.push_back(atoi(argv[i]));
        }
    }

    for (int port : ports)
    {
        std::thread(
            [port]()
            {
                EventLoop currLoop;
                InetAddress addr(server_addr, port);
                Client cli(&currLoop, &addr);
                cli.Start();
                currLoop.loop();
            })
            .detach();
    }

    getchar();

    return 0;
}
int main(int argc, char *argv[])
{
    std::vector<int> ports;
    if (argc < 2)
    {
        ports.push_back(default_port);
    }
    else
    {

        for (int i = 0; i < argc; ++i)
        {
            ports.push_back(atoi(argv[i]));
        }
    }

    for (int port : ports)
    {
        std::thread(
            [port]()
            {
                EventLoop currLoop;
                InetAddress addr(server_addr, port);
                Client cli(&currLoop, &addr);
                cli.Start();
                currLoop.loop();
            })
            .detach();
    }

    getchar();

    return 0;
}*/